#include <bits/stdc++.h>
using namespace std;

struct FenwickTree {
    int size;
    vector<long long> data;

    FenwickTree(int M) : size(M), data(M + 2, 0) {}

    void update(int pos, long long val) {
        pos++;
        while (pos <= size) {
            data[pos] += val;
            pos += pos & -pos;
        }
    }

    long long query(int pos) {
        pos++;
        long long res = 0;
        while (pos > 0) {
            res += data[pos];
            pos -= pos & -pos;
        }
        return res;
    }

    long long range_query(int l, int r) {
        if (l > r) return 0;
        return query(r) - query(l - 1);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    vector<int> A(N);
    for (auto &a : A) {
        cin >> a;
    }

    vector<int> S(N + 1, 0);
    for (int i = 1; i <= N; ++i) {
        S[i] = (S[i - 1] + A[i - 1]) % M;
    }

    FenwickTree ft(M);
    long long ans = 0;
    long long s_prev = 0;

    for (int r = 0; r <= N; ++r) {
        int current = S[r];
        long long cnt_greater = ft.range_query(current + 1, M - 1);
        ans += 1LL * current * r - s_prev + cnt_greater * M;

        ft.update(current, 1);
        s_prev += current;
    }

    cout << ans << '\n';

    return 0;
}